package com.csii.activiti.server.core.service.impl;

import com.csii.activiti.server.core.service.FlowEngineService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricVariableInstance;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class FlowEngineServiceImpl implements FlowEngineService {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private HistoryService historyService;

    @Override
    public Map<String, Object> queryRuVariables(String processInstanceId) {
        Map<String, Object> variables = runtimeService.getVariables(processInstanceId);
        return variables;
    }

    @Override
    public Map<String, Object> queryHiVariables(String processInstanceId) {
        List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId).list();
        Map<String, Object> variables = list.stream().collect(Collectors.toMap(HistoricVariableInstance::getVariableName, HistoricVariableInstance::getValue, (v1, v2) -> v2));
        return variables;
    }
}
